package com.ying.controller.order;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ying.core.response.ApiResponse;
import com.ying.po.order.Wrap;
import com.ying.service.order.WrapService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/api/order/wrap")
@Api(tags = "Wrap接口")
public class WrapController {

    @Autowired
    private WrapService wrapService;

    @GetMapping("/{id}")
    @SaCheckPermission("wrap-add")
    @ApiOperation("根据ID查询wrap")
    public ApiResponse finById(@PathVariable(value = "id") Integer id) {
        return ApiResponse.success(wrapService.findById(id));
    }

    @PostMapping("/create")
    @SaCheckPermission("wrap-post")
    @ApiOperation("创建wrap对象")
    public ApiResponse create(@RequestBody Wrap wrap) {
        // 存储wrap
        Integer id = wrapService.create(wrap);
        return ApiResponse.success(id);
    }


    @DeleteMapping("/delete/{id}")
    @SaCheckPermission("wrap-delete")
    @ApiOperation("根据ID删除")
    public ApiResponse delete(@PathVariable Integer id) {
        wrapService.deleteById(id);
        return ApiResponse.success(null);
    }

    @DeleteMapping("/delete")
    @ApiOperation("根据ID列表批量删除")
    @SaCheckPermission("wrap-delete")
    public ApiResponse deleteByIds(@RequestBody List<Integer> ids) {
        wrapService.deleteByIdInBatch(ids);
        return ApiResponse.success(null);
    }

    @PostMapping("/findbyids")
    @ApiOperation("根据ID列表查询")
    @SaCheckPermission("wrap-get")
    public ApiResponse findByIds(@RequestBody Integer[] ids) {
        List<Wrap> userList = wrapService.findByIdInBatch(Arrays.asList(ids));
        return ApiResponse.success(userList);
    }

    @GetMapping("/find/year/{year}")
    @SaCheckPermission("wrap-get")
    @ApiOperation("查询某年的包装")
    public ApiResponse findOfOneYear(@PathVariable("year") Integer year, @RequestParam("userId") Integer userId) {
        ApiResponse success = ApiResponse.success(wrapService.findOfOneYear(year, userId));
        return success;
    }

    @GetMapping("/find/year")
    @SaCheckPermission("wrap-get")
    @ApiOperation("查询一段时间的包装")
    public ApiResponse findDuringYear(@RequestParam("year1") Integer year1, @RequestParam("year1") Integer year2, @RequestParam("userId") Integer userId) {
        ApiResponse success = ApiResponse.success(wrapService.findDuringYear(year1, year2, userId));
        return success;
    }

    @GetMapping("/price/all")
    @SaCheckPermission("wrap-get")
    @ApiOperation("查询本年所有的wrap价格")
    public ApiResponse getWrapPrice(@RequestParam("userId") Integer userId) {
        return ApiResponse.success(wrapService.getWrapPrice(userId));
    }

}
